package com.moter.estest.handler;

import com.moter.estest.result.R;
import com.moter.estest.util.ExceptionUtils;
import com.moter.estest.util.MoterException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Component
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e){
        log.error(e.getMessage());
        return R.error().message(e.getMessage());
    }

    @ExceptionHandler(MoterException.class)
    @ResponseBody
    public R error(MoterException e){
        log.error(ExceptionUtils.getMessage(e));
        return R.error().code(e.getCode()).message(e.getMessage());
    }
}
